#!/usr/bin/perl
# Version 0.2.1

$testcase = "self_orig_lsa";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 1) {
        print("Usage: $testcase.pl dump_file log_file");
        exit 1;
}

$dumpfile = $ARGV[0];
$logfile = $ARGV[1];

#------------------------------------------------------------------------------

$state = 0;
$sequence = 0;

# Open the dumpfile
$success = 1;
open(DUMPFILE, $dumpfile) or $success = 0;

if ($success eq 1) {
		
	while(<DUMPFILE>) {
		
		if( $state eq 0 ) {
			@l = split(/ /, $_);
			# now look at the time
			@ti = split(/\./, $l[0]);
			@ts = split(/:/, $ti[0]);
			
			$secs = $ts[2] + ($ts[1] + $ts[0] * 60) * 60; # secs contains the sim time in seconds
		
			if ($secs > 395) {
				# now there should come the interesting packets
				$state = 1;
			}
		}
		if( $state eq 1 ) {
			# look if 0.0.0.2 lists 0.0.0.1's Router-LSA in a DD-Packet
			if($_ =~ /0\.0\.0\.2 > (.*) OSPFv2-dd (.*) S (.*) (.*) rtr 0\.0\.0\.1/) {
				# yepp! found the dd packet. save the sequence no.
				$sequence = $3;
				$state = 2;
			}
		}
		if( $state eq 2 ) {
			# wait until 0.0.0.1 requests the lsa for 0.0.0.1
			if($_ =~ /0\.0\.0\.1 > (.*) OSPFv2-ls_req (.*) rtr 0\.0\.0\.1/) {
				# exactly right!
				$state = 3;
			}
		}
		if( $state eq 3 ) {
			# now wait for 0.0.0.2's update for the lsa
			if($_ =~ /0\.0\.0\.2 > (.*) OSPFv2-ls_upd (.*) rtr 0\.0\.0\.1/) {
				# got it
				$state = 4;
			}
		}
		if( $state eq 4 ) {
			# finally wait for 0.0.0.1's update of the LSA with a higher sequence no.
			if($_ =~ /0\.0\.0\.1 > (.*) OSPFv2-ls_upd (.*) S (.*) (.*) rtr 0\.0\.0\.1/) {
				$state = 5;
				if($3 > $sequence) {
					# ok - when this state has been reached, the test is passed.				
					$state = 6;
				}
			}
			
		}
	}
	
	close(DUMPFILE);
	
	if($state eq 6) {
		$verdict = 1;
	} else {
		$verdict = 2;
	}
}

if($verdict eq 1) {
	print("RUT correctly handles an old self-originated LSA. Test PASSED.\n");
} elsif($verdict eq 2) {
	if($state eq 0) {
		print("Test procedure is too short. No relevant results found. Test FAILED.\n");
	} elsif($state eq 1) {
		print("TR doesn't list the RUT's old LSA in DD packets. Test FAILED.\n");
	} elsif($state eq 2) {
		print("RUT does not request its old LSA. Test FAILED.\n");
	} elsif($state eq 3) {
		print("TR doesn't send an update for the RUT's requested LSA. Test FAILED.\n");
	} elsif($state eq 4) {
		print("RUT does not update its LSA. Test FAILED.\n");
	} elsif ($state eq 5) {
		print("RUT does not increase sequence number in update. Test FAILED.\n");
	}
	
} else {
	print("Cannot evaluate test results. Test INCONCLUSIVE.\n");
}

	

#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");